package top.kaoshanji.p1ch05a;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * author kaoshanji
 * ^_^
 * create time 2025/2/11 15:53
 */
public class Reducing {

    private static final Logger logger = LogManager.getLogger(Reducing.class);

    public static void main(String[] args) {

        // 代码清单-p1ch05a-98页a：元素求和
        logger.info("...元素求和...");
        List<Integer> numbers = Arrays.asList(3, 4, 5, 1, 2);
        int sum = numbers.stream().reduce(0, (a, b) -> a + b);
        System.out.println(sum);

        int sum2 = numbers.stream().reduce(0, Integer::sum);
        System.out.println(sum2);


        // 代码清单-p1ch05a-100页a：最大值和最小值
        logger.info("...最大值和最小值...");
        int max = numbers.stream().reduce(0, (a, b) -> Integer.max(a, b));
        System.out.println(max);

        Optional<Integer> min = numbers.stream().reduce(Integer::min);
        min.ifPresent(System.out::println);


    }

}
